﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using System.Web.Mvc;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Newtonsoft.Json;
using AutoMapper;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Presentation.ViewModel.PurchaseModule;

namespace Huirui.Cavan.Presentation.Controller.StockModule
{
    public class StockInOutController : EntityController<StockInOut, string, StockInOutSearch, IStockInOutViewModel, IStockInOutAppService>, IStockInOutController
    {
        private IStockInOutTypeAppService StockInOutTypeAppService { get; set; }

        public override string ControllerName
        {
            get { return ControllerConst.STOCKINOUT; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public StockInOutController()
        {
            this.Model = ModelFactory.CreateModel<IStockInOutViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "stockin_query")]
        public ActionResult AddStockIn(string id, int isin)
        {
            this.Model.IsIn = isin > 0 ? true : false;
            if (id.IsNotEmpty())
            {
                this.Model.Item = this.AppService.GetById(id);
            }

            this.Model.StockInOutTypes.Add(new SelectListItem() { Text = "请选择", Value = "0" });
            IList<StockInOutType> stockInOutTypes = this.StockInOutTypeAppService.GetBySearch(SearchFactory.CreateStockInOutTypeSearch(this.Model.IsIn)).ToList();
            foreach (StockInOutType siot in stockInOutTypes)
            {
                if (this.Model.Item.IsNotNull() && this.Model.Item.Type.Id == siot.Id)
                {
                    this.Model.StockInOutTypes.Add(new SelectListItem() { Text = siot.Name, Value = siot.Id.ToString(), Selected = true });
                }
                else
                {
                    this.Model.StockInOutTypes.Add(new SelectListItem() { Text = siot.Name, Value = siot.Id.ToString() });
                }
            }
            if (this.Model.IsIn)
            {
                return View("~/Views/Stock/v1/addStockIn.cshtml", this.Model);
            }
            return View("~/Views/Stock/v1/addStockOut.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "stockin_edit")]
        public JsonResult AddStockIn(StockInOutViewModel model)
        {
            BoolResult jsonResult = null;
            IList<StockInOutDetail> stockInOutDetails = JsonConvert.DeserializeObject<IList<StockInOutDetail>>(model.StockInDetails);
            model.Item.StockInOutDetails = stockInOutDetails;
            model.Item.Type = new StockInOutType() { Id = model.Type };

            BoolGenericResult<StockInOut> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [HttpGet]
        public ActionResult GetStockInOuts()
        {
            return View("~/Views/Stock/v1/stockinoutList.cshtml");
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        public JsonResult GetStockInOuts(StockInOutViewModel model)
        {
            StockInOutJsonViewModel json = new StockInOutJsonViewModel();
            json.rows = new List<StockInOutJson>();

            long total = 0;
            IList<StockInOut> stockInOuts = this.AppService.GetBySearch(new StockInOutSearch() {  StartDate = model.SearchParam.StartDate,
             EndDate = model.SearchParam.EndDate, SupplierCode = model.SearchParam.SupplierCode, ID = model.SearchParam.ID }, out total).ToList();
            foreach (StockInOut sio in stockInOuts)
            {
                StockInOutJson cj = Mapper.Map<StockInOut, StockInOutJson>(sio);
                json.rows.Add(cj);
            }
            json.total = total;

            return Json(json, "application/json");
        }
    }
}
